home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWNotifn / Sources / FWNotifr.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  5.6 KB  |  179 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWNotifr.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWNOTIFR_H
  13. #include "FWNotifr.h"
  14. #endif
  15.  
  16. #ifndef FWRECEVR_H
  17. #include "FWRecevr.h"
  18. #endif
  19.  
  20. #ifndef FWNOTIFN_H
  21. #include "FWNotifn.h"
  22. #endif
  23.  
  24. #ifndef FWINTERE_H
  25. #include "FWIntere.h"
  26. #endif
  27.  
  28. #ifndef FWNOTDEF_H
  29. #include "FWNotDef.h"
  30. #endif
  31.  
  32. #ifndef FWTCOLL_H
  33. #include "FWTColl.h"
  34. #endif
  35.  
  36. #ifndef FWSOMENV_H
  37. #include "FWSOMEnv.h"
  38. #endif
  39.  
  40. //========================================================================================
  41. //    Template Instantiations
  42. //========================================================================================
  43.  
  44. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollectionIterator, FW_SPrivInterestReceiver)
  45. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollection, FW_SPrivInterestReceiver)
  46.  
  47. #ifdef FW_USE_TEMPLATE_PRAGMAS
  48.  
  49. #pragma template_access public
  50. #pragma template FW_TOrderedCollection<FW_SPrivInterestReceiver>
  51. #pragma template FW_TOrderedCollectionIterator<FW_SPrivInterestReceiver>
  52.  
  53. #endif
  54.  
  55. //========================================================================================
  56. // CLASS FW_MNotifier
  57. //========================================================================================
  58.  
  59. FW_DEFINE_CLASS_M0(FW_MNotifier)
  60. FW_DEFINE_AUTO(FW_MNotifier)
  61.  
  62. //----------------------------------------------------------------------------------------
  63. // FW_MNotifier::FW_MNotifier
  64. //----------------------------------------------------------------------------------------
  65.  
  66. FW_MNotifier::FW_MNotifier() :
  67.     fInterestList()
  68. {
  69. }
  70.  
  71. //----------------------------------------------------------------------------------------
  72. // FW_MNotifier::~FW_MNotifier
  73. //----------------------------------------------------------------------------------------
  74.  
  75. FW_MNotifier::~FW_MNotifier()
  76. {
  77.     // Notify the receivers that this notifier is being deleted
  78.     // (used for instance to remove a RadioCluster when all radio buttons are gone)
  79.     FW_CNotification notifierDeleted(FW_CInterest(this, FW_kNotifierDeletedMsg));
  80.  
  81.     FW_TOrderedCollectionIterator<FW_SPrivInterestReceiver> ite(&fInterestList);
  82.     FW_SPrivInterestReceiver* pair = ite.First();
  83.     
  84.     FW_SOMEnvironment    ev;                
  85.  
  86.     while (pair) 
  87.     {
  88.         // Safer iteration, because the receiver may be deleted in the middle
  89.         // [LSD] Warning: still dangerous in case the notifier has more than 1 interest
  90.         FW_SPrivInterestReceiver* next = ite.Next();
  91.         pair->fReceiver->HandleNotification(ev, notifierDeleted);
  92.         pair = next;
  93.     }
  94.  
  95.     // ----- Delete all left over interest pair
  96.     while ((pair = fInterestList.First()) != NULL)
  97.     {
  98.         // RemoveInterest will call RemoveReceiver
  99.         pair->fReceiver->RemoveInterest(*pair->fInterest);
  100.     }
  101. }
  102.     
  103. //----------------------------------------------------------------------------------------
  104. // FW_MNotifier::Notify
  105. //----------------------------------------------------------------------------------------
  106.  
  107. void FW_MNotifier::Notify(Environment* ev, const FW_CNotification ¬ification)
  108. {
  109.     FW_TOrderedCollectionIterator<FW_SPrivInterestReceiver> ite(&fInterestList);
  110.     FW_SPrivInterestReceiver* pair = ite.First();
  111.     while (pair) 
  112.     {
  113.         // Get the next pair before doing any action in case the object is destroyed
  114.         // in the middle (for instance: OK button closing a dialog)
  115.         // [LSD] Warning: still dangerous in case the notifier has more than 1 interest
  116.         FW_SPrivInterestReceiver* next = ite.Next();
  117.         
  118.         if (*pair->fInterest == *notification.GetInterest())
  119.             pair->fReceiver->HandleNotification(ev, notification);
  120.         
  121.         pair = next;
  122.     }
  123. }
  124.     
  125. //----------------------------------------------------------------------------------------
  126. // FW_MNotifier::AddReceiver
  127. //----------------------------------------------------------------------------------------
  128.  
  129. void FW_MNotifier::AddReceiver(FW_MReceiver* receiver, FW_CInterest* interest)
  130. {
  131.     FW_SPrivInterestReceiver* pair = new FW_SPrivInterestReceiver;
  132.     pair->fReceiver = receiver;
  133.     pair->fInterest = interest;
  134.     
  135.     fInterestList.AddLast(pair);
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // FW_MNotifier::RemoveReceiver
  140. //----------------------------------------------------------------------------------------
  141.  
  142. void FW_MNotifier::RemoveReceiver(FW_MReceiver* receiver, const FW_CInterest& interest)
  143. {
  144.     FW_SPrivInterestReceiver* pair;
  145.     
  146.     FW_TOrderedCollection<FW_SPrivInterestReceiver> temp;
  147.     
  148.     FW_TOrderedCollectionIterator<FW_SPrivInterestReceiver> ite1(&fInterestList);
  149.     for (pair = ite1.First(); ite1.IsNotComplete(); pair = ite1.Next())
  150.     {
  151.         if (*pair->fInterest == interest && pair->fReceiver == receiver)
  152.             temp.AddLast(pair);        
  153.     }
  154.  
  155.     FW_TOrderedCollectionIterator<FW_SPrivInterestReceiver> ite2(&temp);
  156.     for (pair = ite2.First(); ite2.IsNotComplete(); pair = ite2.Next())
  157.     {
  158.         fInterestList.Remove(pair);
  159.         delete pair;
  160.     }
  161.     
  162.     // let the temp::__dt call RemoveAll
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_MNotifier::IsConnectedTo
  167. //----------------------------------------------------------------------------------------
  168.  
  169. FW_Boolean FW_MNotifier::IsConnectedTo(FW_MReceiver* receiver, const FW_CInterest& interest)
  170. {
  171.     FW_SPrivInterestReceiver* pair;
  172.     FW_TOrderedCollectionIterator<FW_SPrivInterestReceiver> ite1(&fInterestList);
  173.     for (pair = ite1.First(); ite1.IsNotComplete(); pair = ite1.Next())
  174.     {
  175.         if (*pair->fInterest == interest && pair->fReceiver == receiver)
  176.             return TRUE;        
  177.     }
  178.     return FALSE;
  179. }